package com.aricwang.easyprofile;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

import com.aricwang.easyprofile.help.HelpActivity;
import com.aricwang.easyprofile.place.PlaceListActivity;
import com.aricwang.easyprofile.profile.ProfileListActivity;
import com.aricwang.easyprofile.rule.RuleListActivity;
import com.aricwang.easyprofile.service.ProfileService;

/*
 * The main activity of the app.
 */
public class EasyProfileActivity extends TabActivity {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
		setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent(this, ProfileListActivity.class);
     
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("profiles")
        		.setIndicator("Profiles", res.getDrawable(android.R.drawable.ic_menu_preferences))
                .setContent(intent);
        tabHost.addTab(spec);
        
        // Do the same for the other tabs
        intent = new Intent().setClass(this, RuleListActivity.class);
        spec = tabHost.newTabSpec("rules")
        		.setIndicator("Rules", res.getDrawable(android.R.drawable.ic_menu_rotate))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, PlaceListActivity.class);
        spec = tabHost.newTabSpec("places")
        		.setIndicator("Places", res.getDrawable(android.R.drawable.ic_menu_myplaces))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, HelpActivity.class);
        spec = tabHost.newTabSpec("help")
        		.setIndicator("Help", res.getDrawable(android.R.drawable.ic_menu_help))
                .setContent(intent);
        tabHost.addTab(spec);
        
        tabHost.setCurrentTab(0);
    
        // start the service
		ProfileService.startService(this);		
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_exit:     
            	exitApplication();
                break;
            default:
            	break;
        }
        
        return true;
    }
    
    private void exitApplication() {
    	stopService(new Intent(this, ProfileService.class));
    	
    	Intent intent = new Intent(Intent.ACTION_MAIN);
    	intent.addCategory(Intent.CATEGORY_HOME);
    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    	startActivity(intent);
    }
}